<template>
  <div class="messages-container">
    <el-card>
      <div class="header">
        <h2>消息中心</h2>
        <div class="actions">
          <el-button type="primary" icon="el-icon-plus" @click="composeMessage">写消息</el-button>
          <el-button icon="el-icon-refresh" @click="refreshMessages">刷新</el-button>
        </div>
      </div>
    </el-card>

    <el-row :gutter="20" class="mt-20">
      <el-col :span="6">
        <el-card class="folders-card">
          <el-menu
              :default-active="activeFolder"
              class="folders-menu"
              @select="handleFolderSelect"
          >
            <el-menu-item index="inbox">
              <i class="el-icon-message"></i>
              <span>收件箱</span>
              <el-badge v-if="unreadCount > 0" :value="unreadCount" class="badge" />
            </el-menu-item>
            <el-menu-item index="sent">
              <i class="el-icon-s-promotion"></i>
              <span>已发送</span>
            </el-menu-item>
            <el-menu-item index="drafts">
              <i class="el-icon-document"></i>
              <span>草稿箱</span>
            </el-menu-item>
            <el-menu-item index="trash">
              <i class="el-icon-delete"></i>
              <span>回收站</span>
            </el-menu-item>
          </el-menu>
        </el-card>
      </el-col>

      <el-col :span="18">
        <el-card>
          <div class="messages-header">
            <h3>{{ folderTitles[activeFolder] }}</h3>
            <el-input
                v-model="searchText"
                placeholder="搜索消息"
                prefix-icon="el-icon-search"
                clearable
                style="width: 300px;"
            />
          </div>

          <el-table :data="filteredMessages" style="width: 100%" height="600">
            <el-table-column width="50">
              <template #default="scope">
                <i
                    v-if="activeFolder === 'inbox' && !scope.row.read"
                    class="el-icon-star-off unread-icon"
                ></i>
                <i
                    v-else-if="activeFolder === 'inbox' && scope.row.read"
                    class="el-icon-star-off read-icon"
                ></i>
              </template>
            </el-table-column>
            <el-table-column prop="sender" label="发件人" width="180" v-if="activeFolder !== 'sent'" />
            <el-table-column prop="recipient" label="收件人" width="180" v-if="activeFolder === 'sent'" />
            <el-table-column prop="title" label="主题">
              <template #default="scope">
                <span :class="{ 'unread-text': activeFolder === 'inbox' && !scope.row.read }">
                  {{ scope.row.title }}
                </span>
              </template>
            </el-table-column>
            <el-table-column prop="time" label="时间" width="180" />
            <el-table-column label="操作" width="120" fixed="right">
              <template #default="scope">
                <el-button v-if="activeFolder !== 'trash'" type="text" @click="viewMessage(scope.row)">查看</el-button>
                <el-button type="text" @click="deleteMessage(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <div class="pagination">
            <el-pagination
                v-model:current-page="currentPage"
                :page-size="pageSize"
                layout="total, prev, pager, next"
                :total="totalMessages"
                @current-change="handlePageChange"
            />
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 消息查看对话框 -->
    <el-dialog v-model="messageDialogVisible" :title="currentMessage.title" width="60%">
      <div class="message-header">
        <div><strong>发件人:</strong> {{ currentMessage.sender }}</div>
        <div><strong>收件人:</strong> {{ currentMessage.recipient }}</div>
        <div><strong>时间:</strong> {{ currentMessage.time }}</div>
      </div>
      <el-divider />
      <div class="message-content">
        {{ currentMessage.content }}
      </div>
      <template #footer>
        <el-button @click="messageDialogVisible = false">关闭</el-button>
        <el-button type="primary" v-if="activeFolder === 'inbox'" @click="replyMessage">回复</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'

export default {
  name: 'Messages',
  setup() {
    const activeFolder = ref('inbox')
    const currentPage = ref(1)
    const pageSize = ref(10)
    const totalMessages = ref(25)
    const searchText = ref('')
    const messageDialogVisible = ref(false)

    const folderTitles = {
      inbox: '收件箱',
      sent: '已发送',
      drafts: '草稿箱',
      trash: '回收站'
    }

    const inboxMessages = ref([
      {
        id: 1,
        sender: '系统通知',
        recipient: '管理员',
        title: '系统升级通知',
        content: '系统将于本周六凌晨2:00-4:00进行升级维护，期间系统将无法访问，请提前做好准备。',
        time: '2023-05-18 09:30',
        read: false
      },
      {
        id: 2,
        sender: '人力资源部',
        recipient: '管理员',
        title: '新员工入职通知',
        content: '张三(EMP10025)将于2023-06-01入职，担任前端开发工程师，请做好相关准备工作。',
        time: '2023-05-19 14:15',
        read: true
      },
      {
        id: 3,
        sender: '财务部',
        recipient: '管理员',
        title: '工资条发放通知',
        content: '2023年4月份工资条已发放，请登录系统查看。',
        time: '2023-05-20 10:45',
        read: false
      }
    ])

    const sentMessages = ref([
      {
        id: 101,
        sender: '管理员',
        recipient: '技术部',
        title: '关于项目进度的提醒',
        content: '请各项目组在本周五前提交项目进度报告，包括当前完成情况、遇到的问题和下周计划。',
        time: '2023-05-15 16:20',
        read: true
      }
    ])

    const unreadCount = computed(() => {
      return inboxMessages.value.filter(msg => !msg.read).length
    })

    const currentMessage = reactive({
      id: null,
      sender: '',
      recipient: '',
      title: '',
      content: '',
      time: ''
    })

    const filteredMessages = computed(() => {
      const messages = {
        inbox: inboxMessages.value,
        sent: sentMessages.value,
        drafts: [],
        trash: []
      }[activeFolder.value]

      return messages.filter(msg => {
        return msg.title.includes(searchText.value) ||
            msg.content.includes(searchText.value) ||
            msg.sender.includes(searchText.value) ||
            msg.recipient.includes(searchText.value)
      })
    })

    const handleFolderSelect = (folder) => {
      activeFolder.value = folder
      currentPage.value = 1
    }

    const viewMessage = (message) => {
      Object.assign(currentMessage, message)
      messageDialogVisible.value = true

      // 标记为已读
      if (activeFolder.value === 'inbox') {
        const msg = inboxMessages.value.find(m => m.id === message.id)
        if (msg) msg.read = true
      }
    }

    const deleteMessage = (message) => {
      ElMessage.success('消息已删除')
      // 实际项目中这里会从列表中移除
    }

    const replyMessage = () => {
      ElMessage.info('回复消息')
      messageDialogVisible.value = false
    }

    const composeMessage = () => {
      ElMessage.info('撰写新消息')
    }

    const refreshMessages = () => {
      ElMessage.success('消息列表已刷新')
    }

    const handlePageChange = (page) => {
      currentPage.value = page
    }

    return {
      activeFolder,
      currentPage,
      pageSize,
      totalMessages,
      searchText,
      messageDialogVisible,
      folderTitles,
      currentMessage,
      unreadCount,
      filteredMessages,
      handleFolderSelect,
      viewMessage,
      deleteMessage,
      replyMessage,
      composeMessage,
      refreshMessages,
      handlePageChange
    }
  }
}
</script>

<style scoped>
.messages-container {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.mt-20 {
  margin-top: 20px;
}

.folders-card {
  height: 100%;
}

.folders-menu {
  border-right: none;
}

.badge {
  margin-left: 10px;
}

.messages-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.unread-icon {
  color: #F56C6C;
}

.read-icon {
  color: #C0C4CC;
}

.unread-text {
  font-weight: bold;
}

.message-header {
  margin-bottom: 20px;
  line-height: 1.8;
}

.message-content {
  line-height: 1.8;
  white-space: pre-wrap;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>